﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponTrialEffect : MonoBehaviour {

    public Transform TrialStartTrans;

    public Transform TrialEndTrans;

    /// <summary>
    /// Mesh中形成三角形的索引
    /// </summary>
    public int[] Indices;
    /// <summary>
    /// Mesh中的UV值
    /// </summary>
    public Vector2[] UVs;
    /// <summary>
    /// Mesh拥有的点
    /// </summary>
    public Vector3[] Vertices;

    public Mesh TrialMesh;

    public Material TrialMaterial;

    public List<TrialItem> TrialItemList = new List<TrialItem>();
    /// <summary>
    /// 武器移动记录的点数目的最大值
    /// </summary>
    public int MaxCount=30;
    /// <summary>
    /// 粒度，采样数
    /// </summary>
    public int Granularity=14;
    /// <summary>
    /// 拖影宽度
    /// </summary>
    public float TrailWidth=1;


    void Start () {
        TrialMesh = new Mesh();

        Init();

        UpdateIndices();
    }

    void Init()
    {
        Vertices = new Vector3[3* MaxCount];
        UVs = new Vector2[3 * MaxCount];
        Indices = new int[Granularity * 9];
    }

    void UpdateIndices()
    {
        
        for (int i = 0; i < Granularity; i++)
        {
            int baseIdx =  i * 3;
            int nextBaseIdx = (i + 1) * 3;

            int iidx =  i * 9;

            //triangle left
            Indices[iidx + 0] = baseIdx;
            Indices[iidx + 1] = baseIdx + 2;
            Indices[iidx + 2] = baseIdx + 4;

            Indices[iidx + 3] = baseIdx;
            Indices[iidx + 4] = baseIdx + 3;
            Indices[iidx + 5] = baseIdx + 4;

            Indices[iidx + 6] = baseIdx + 2;
            Indices[iidx + 7] = baseIdx + 4;
            Indices[iidx + 8] = baseIdx + 5;
        }
    }

    void UpdateVertex()
    {
        for (int i = 0; i < MaxCount; i++)
        {
            int baseIdx =   i * 3;

            float uvSegment = (float)i / Granularity;
            
            Vector2 uvCoord = Vector2.zero;

            int index = i * MaxCount / Granularity;
            Vector3 pos = TrialItemList[i].StartPoint + (TrialItemList[i].EndPoint - TrialItemList[i].StartPoint) / 2;

            Vector3 dir = TrialItemList[i].EndPoint - TrialItemList[i].StartPoint;
            Vector3 pos0 = TrialItemList[i].StartPoint;
            Vector3 pos1 = TrialItemList[i].EndPoint;


            // pos0
            Vertices[baseIdx] = pos0;
            //pool.Colors[baseIdx] = MyColor;
            uvCoord.x = 0f;
            uvCoord.y = uvSegment;
            UVs[baseIdx] = uvCoord;

            //pos
            Vertices[baseIdx + 1] = pos;
            //pool.Colors[baseIdx + 1] = MyColor;
            uvCoord.x = 0.5f;
            uvCoord.y = uvSegment;
            UVs[baseIdx + 1] = uvCoord;

            //pos1
            Vertices[baseIdx + 2] = pos1;
            //Colors[baseIdx + 2] = MyColor;
            uvCoord.x = 1f;
            uvCoord.y = uvSegment;
            UVs[baseIdx + 2] = uvCoord;
        }
    }
	
	// Update is called once per frame
	void Update () {

        TrialItem item = new TrialItem();
        item.StartPoint = TrialStartTrans.position;
        item.EndPoint = TrialEndTrans.position;
        TrialItemList.Insert(0,item);
        if (TrialItemList.Count>=MaxCount)
        {
            TrialItemList.RemoveAt(TrialItemList.Count - 1);
        }
        UpdateVertex();
    }

    private void LateUpdate()
    {
        TrialMesh.vertices = Vertices;
        TrialMesh.uv = UVs;
        TrialMesh.triangles = Indices;

        //TrialMesh.vertices = new Vector3[] { new Vector3(1,1,0),new Vector3(1,0,1),new Vector3(0,1,0)};
        ////TrialMesh.uv = UVs;
        //TrialMesh.triangles = new int[] { 0,1,2};

        Graphics.DrawMesh(TrialMesh, Matrix4x4.identity, TrialMaterial, gameObject.layer, null, 0, null, false, false);
    }
}
